/*
 *  Copyright (C) 2012 Компания ВИТ. web: http://diasled.ru Е-mail:  vit@diasled.ru
 * 
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package ru.apertum.diasled.model;

import java.io.Serializable;
import java.util.LinkedList;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import ru.apertum.diasled.common.Uses;
import ru.apertum.diasled.common.Uses.ClientException;
import ru.apertum.diasled.db.HibernateUtil;

/**
 * Обслелование.
 * Врач проводит обследование - создается этот класс.
 * @author Evgeniy Egorov, Aperum Projects
 */
@Entity
@Table(name = "examination")
public class Examination implements Serializable {

    @Id
    @Column(name = "id")
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }
    //@OneToOne(fetch = FetchType.EAGER)
    //@JoinColumn(name = "visit_id", nullable = false)
    @ManyToOne
    @JoinColumn(name = "visit_id")
    private Visit visit;

    public Visit getVisit() {
        return visit;
    }

    public void setVisit(Visit visit) {
        this.visit = visit;
    }
    @Column(name = "shoes_type", nullable = false)
    private Integer shoesType;

    public Integer getShoesType() {
        return shoesType;
    }

    public void setShoesType(Integer shoesType) {
        this.shoesType = shoesType;
    }
    @Column(name = "inside_element", nullable = false)
    private Integer insideElement;

    public Integer getInsideElement() {
        return insideElement;
    }

    public void setInsideElement(Integer insideElement) {
        this.insideElement = insideElement;
    }
    @Column(name = "exam_type", nullable = false)
    private Integer examType;

    public Integer getExamType() {
        return examType;
    }

    public void setExamType(Integer examType) {
        this.examType = examType;
    }
    @Column(name = "comments", nullable = false, length = 1024)
    private String comments = "";

    public String getComments() {
        return comments;
    }

    public void setComments(String comments) {
        this.comments = comments;
    }
    
    @Column(name = "steps_per_minute", nullable = false)
    private Integer stepsРerMinute = 0;

    public Integer getStepsРerMinute() {
        return stepsРerMinute;
    }

    public void setStepsРerMinute(Integer stepsРerMinute) {
        this.stepsРerMinute = stepsРerMinute;
    }
    
    /**
     * Длительность эксперимента в секундах
     */
    @Column(name = "duration", nullable = false)
    private Integer duration = 0;

    public Integer getDuration() {
        return duration;
    }

    public void setDuration(Integer duration) {
        this.duration = duration;
    }
    
    @Column(name = "sys_num", nullable = false)
    private Integer sysNum = 0;

    public Integer getSysNum() {
        return sysNum;
    }

    public void setSysNum(Integer sysNum) {
        this.sysNum = sysNum;
    }

    //***********************************************************************************************************************************************************************
    //********* DAO
    //***********************************************************************************************************************************************************************
    synchronized static public void saveExam(Examination exam) throws ClientException {
        final Session sess = HibernateUtil.getInstance().getSession();
        sess.beginTransaction();
        try {
            sess.saveOrUpdate(exam);
            sess.getTransaction().commit();
        } catch (Exception ex) {
            Uses.log.logger.error("Ошибка сохранения параметров обследования.", ex);
            sess.getTransaction().rollback();
            throw new Uses.ClientException("Ошибка сохранения параметров обследования.", ex);
        } finally {
            sess.close();
        }
    }

    synchronized static public void removeExam(Examination exam) {
        final Session sess = HibernateUtil.getInstance().getSession();
        sess.beginTransaction();
        try {
            sess.delete(exam);
            sess.getTransaction().commit();
        } catch (Exception ex) {
            Uses.log.logger.error("Ошибка удаления параметров обследования.", ex);
            sess.getTransaction().rollback();
        } finally {
            sess.close();
        }
    }

    synchronized static public LinkedList<Examination> getExams(Visit visit) {
        final Session sess = HibernateUtil.getInstance().getSession();
        try {
            return new LinkedList<>(sess.createCriteria(Examination.class).add(Restrictions.eq("visit", visit)).list());
        } finally {
            sess.close();
        }
    }
}
